package com.easy.service.impl;

import com.easy.bean.User;
import com.easy.mapper.UserMapper;
import com.easy.service.RegisterService;
import com.easy.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements RegisterService {


    @Autowired
    UserMapper userMapper;

    @Override
    public void register(User user) {
        // 先进行基本字段验证
        if (user.getUsername() == null || user.getUsername().trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
            throw new IllegalArgumentException("密码不能为空");
        }
        if (user.getId_card() == null || user.getId_card().trim().isEmpty()) {
            throw new IllegalArgumentException("身份证不能为空");
        }
        if (user.getPhone() == null || user.getPhone().trim().isEmpty()) {
            throw new IllegalArgumentException("手机号不能为空");
        }

        if (user.getReadpassword() == null || user.getReadpassword().trim().isEmpty()) {
            throw new IllegalArgumentException("确认密码不能为空");
        }
        // 密码一致性检查
        if (!user.getPassword().equals(user.getReadpassword())) {
            throw new IllegalArgumentException("密码不一致");
        }

        // 在调用服务前加密密码
        user.setPassword(PasswordUtil.getEncryption(user.getPassword()));


        userMapper.add(user);

    }

    @Override
    public User getByPhone(String phone) {
        return userMapper.getByPhone(phone);
    }
}
